package bulk_param.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JOptionPane;

import bulk_param.modele.OuverturePointModel;


public class ModifNumSpeDao {
	
	public OuverturePointModel ouverturePointModel;
	private String userid = "arbor";
    private String password = "arbor123";
    private String pilote = "oracle.jdbc.driver.OracleDriver";
    private String url = "jdbc:oracle:thin:@10.1.3.136:1522:cc_cust";  	

	private static Connection con;

     // constructor 
	public ModifNumSpeDao(){
		this.ouverturePointModel= new OuverturePointModel();
	}

	public Connection getConnection(){
				
		try {
			Class.forName(pilote);	

		} catch(java.lang.ClassNotFoundException e) {
			System.err.print("ClassNotFoundException: ");
			System.err.println(e.getMessage());
		}

		try {
			con = DriverManager.getConnection(url, userid, password);
			System.out.println ("Ouverture Connection");     
		} catch(SQLException ex) {
			System.err.println("SQLException: " + ex.getMessage());
		}
		
		return con;
	}
	
	static  public  void  closeConnection(){         
		System.out.println ("Colse DBConnection.");          
		        
				
		try {
			con.close();             
			System.out.println ("Fermeture Connection");        
			}         
		catch (Exception ex) {             
			System.out.println ("ERROR :"+ex.getMessage());             
			ex.printStackTrace();        
			}    
		} 
	
	//Verification de l'existance d'un point
	public boolean isPointExist(String pointZABPQ){
		getConnection();
			boolean isPointExist =false;
			try{
				String sql="SELECT point FROM USAGE_POINTS "
						+"WHERE point='"+pointZABPQ+"' "
						+"AND inactive_dt is null";
				
				// Create a prepared statement
	 			Statement s = con.createStatement();

				ResultSet rs = s.executeQuery(sql);	
				while(rs.next()){ 
				if (rs.getString("point") != null){  
					isPointExist= true;	
				}else{
					isPointExist= false;	
				}
				}
			}
			catch(Exception e){
				System.out.println(e);
			}
			return isPointExist;
	}
	
	
	
	public String numberJurisdiction(String description_text){
		
		String access_region = " ";
		
		try{
			String sql="SELECT DISTINCT uj.access_region_target "
						+"FROM DESCRIPTIONS d, JURISDICTIONS j , USAGE_JURISDICTION uj "
						+"WHERE j.description_code = d.description_code "
						+"AND j.JURISDICTION_CLASS IN (71,72) " 	
						+"AND d.description_text='"+description_text+"' "
						+"AND j.JURISDICTION=uj.JURISDICTION "
						+"AND uj.INACTIVE_DT IS NULL "
						+"AND d.language_code = 2 ";
			
			// Create a prepared statement
 			Statement s = con.createStatement();

			ResultSet rs = s.executeQuery(sql);	
			while(rs.next()){
				access_region = rs.getString("access_region_target");
			}
		}
		catch(Exception e){
			System.out.println(e);
		}
		return access_region;
	}
	
	//search old tarif
	public String oldTarif(String point){
		
		String description_text = " ";
		
		try{
			String sql="SELECT Distinct upt.DISPLAY_VALUE "
					+"FROM USAGE_POINTS_TEXT upt, USAGE_POINTS up "
					+"WHERE up.point='"+point+"' "
					+"AND upt.point_id=up.point_id "
					+"AND inactive_dt IS NULL ";
			
			// Create a prepared statement
 			Statement s = con.createStatement();

			ResultSet rs = s.executeQuery(sql);	
			while(rs.next()){
				description_text = rs.getString("display_value");
			}
		}
		catch(Exception e){
			System.out.println(e);
		}
		return description_text;
	}
		
	
	//Ouverture NumSpe : appel a la procedure sotckée
		public String openPointNumSpe(int pointClass, String pointZABPQ,int isPrefix,Date date,String accessRegion,String zLT,int entreprise,String typeAction){
			getConnection();
			String success="ok";
			try{
				DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");				
				date.setSeconds(0);
				date.setMinutes(0);
				date.setHours(0);		
				String dat = dateFormat.format(date);
				System.out.println("MA DATE : "+dat);
				System.out.println("Appel à la procedure stocké OPEN_CONTACT_NUM_SPE");			
				CallableStatement cStmt = con.prepareCall("{call CPTL_CONFIG_CC.OPEN_CONTACT_NUM_SPE(?,?,?,?,?,?,?,?)}");	
				
				cStmt.setString(1, pointZABPQ);
			    cStmt.setInt(2, isPrefix);
			    System.out.println("MA DATE : "+dat);
			    cStmt.setInt(3, pointClass);
			    cStmt.setString(4, dat);
			    cStmt.setString(5, accessRegion);
			    cStmt.setInt(6, entreprise);
			    cStmt.setString(7, zLT);
			    cStmt.setString(8, typeAction);
			    cStmt.executeUpdate();
			    			 
				System.out.println("OUVERTURE SUCCES");	
				
			}
			catch(Exception e){
				System.out.println(e);
				success="ko";
				JOptionPane.showMessageDialog(null, e, "Attention", JOptionPane.WARNING_MESSAGE);
			}
			return success;
		}
		
		//ComboBox Point Region International
		public String openPointNumSpeplus1sec(int pointClass, String pointZABPQ,int isPrefix,Date date,String accessRegion,String zLT,int entreprise,Timestamp dateInBase,String typeAction){
			getConnection();
			String success="ok";
			System.out.println("jappele celui décalé a une seconde");
			try{		
				DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
				if (date.getDate()==dateInBase.getDate()&& date.getMonth()==dateInBase.getMonth()&& date.getYear()==dateInBase.getYear()){
					System.out.println(" seconde + 1 de la date en base : ");
					date.setSeconds(dateInBase.getSeconds()+1);
					date.setMinutes(dateInBase.getMinutes());
					date.setHours(dateInBase.getHours());
				} else{
					date.setSeconds(0);
					date.setMinutes(0);
					date.setHours(0);
				}			
				String dat = dateFormat.format(date);
				System.out.println("MA DATE : "+dat);
				System.out.println("Appel à la procedure stocké OPEN_CONTACT_NUM_SPE");			
				CallableStatement cStmt = con.prepareCall("{call CPTL_CONFIG_CC.OPEN_CONTACT_NUM_SPE(?,?,?,?,?,?,?,?)}");				

				cStmt.setString(1, pointZABPQ);
			    cStmt.setInt(2, isPrefix);
			    System.out.println("MA DATE : "+dat);
			    cStmt.setInt(3, pointClass);
			    cStmt.setString(4, dat);
			    cStmt.setString(5, accessRegion);
			    cStmt.setInt(6, entreprise);
			    cStmt.setString(7, zLT);
			    cStmt.setString(8, typeAction);
			    System.out.println("String type action apres");
			    cStmt.executeUpdate();
			    			 
				System.out.println("OUVERTURE SUCCES");	
				
			}
			catch(Exception e){
				System.out.println(e);
				success="ko";
				JOptionPane.showMessageDialog(null, e, "Attention", JOptionPane.WARNING_MESSAGE);
			}
			
			return success;
		}
		
		public boolean isDateBeforeActive(String pointZABPQ,Date date) throws ParseException{
			//Create Connection to the Oracle Database
			getConnection();
			boolean isDateBefore =false;
			Date dateBase =null;
			Date dateChosen=null;
			String dat2=null;
			try{
				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				String dat = dateFormat.format(date);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			
				String sql="SELECT active_dt FROM USAGE_POINTS "
						+"WHERE point ='"+pointZABPQ+"' "
						+"AND inactive_dt is null ";
				
				// Create a prepared statement
	 			Statement s = con.createStatement();
				ResultSet rs = s.executeQuery(sql);	
				
				while(rs.next()){ 			
					dat2=rs.getString("active_dt");		
				}
				
				System.out.println("ma date parsée : " +dat2);
				dateBase=sdf.parse(dat2);
				dateChosen = sdf.parse(dat);
				
				System.out.println("date base "+sdf.format(dateBase));
				System.out.println("date choisie "+sdf.format(dateChosen));

				if(dateBase.compareTo(dateChosen)>0){
					System.out.println("date inferieur à celui en base");
					isDateBefore= true;
				}else if(dateBase.compareTo(dateChosen)<=0){
					System.out.println("date superieur à celui en base");
					isDateBefore= false;
				}
			}
			catch(Exception e){
				System.out.println(e);
			}
			return isDateBefore;
		}
		
		public boolean isDateBeforeInactive(String pointZABPQ,Date date) throws ParseException{
			//Create Connection to the Oracle Database
			getConnection();
			boolean isDateBefore =false;
			Date dateBase =null;
			Date dateChosen=null;
			String dat2=null;
			try{
				DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
				String dat = dateFormat.format(date);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			
				String sql="SELECT inactive_dt FROM USAGE_POINTS "
						+"WHERE point ='"+pointZABPQ+"'";
				
				// Create a prepared statement
	 			Statement s = con.createStatement();
				ResultSet rs = s.executeQuery(sql);	
				
				while(rs.next()){ 			
					dat2=rs.getString("inactive_dt");		
				}
				
				System.out.println("ma date parsée : " +dat2);
				dateBase=sdf.parse(dat2);
				dateChosen = sdf.parse(dat);
				
				System.out.println("date base "+sdf.format(dateBase));
				System.out.println("date choisie "+sdf.format(dateChosen));

				if(dateBase.compareTo(dateChosen)>0){
					System.out.println("date inferieur à celui en base");
					isDateBefore= true;
				}else if(dateBase.compareTo(dateChosen)<=0){
					System.out.println("date superieur à celui en base");
					isDateBefore= false;
				}
			}
			catch(Exception e){
				System.out.println(e);
			}
			return isDateBefore;
		}
		
		public Timestamp dateInBaseActive(String pointZABPQ) throws ParseException{
			//Create Connection to the Oracle Database
			getConnection();
			Timestamp date =null;
			try{
				System.out.println("get Date Active");
				
				String sql="SELECT active_dt FROM USAGE_POINTS "
						+"WHERE point='"+pointZABPQ+"' "
						+"AND inactive_dt is null ";
				
				// Create a prepared statement
	 			Statement s = con.createStatement();
				ResultSet rs = s.executeQuery(sql);	
				
				while(rs.next()){ 	
					date=rs.getTimestamp("active_dt");		
				}
				System.out.println("date en base : "+date);
			}
			catch(Exception e){
				System.out.println(e);
			}
			return date;
		}
		
		public Timestamp dateInBaseInactive(String pointZABPQ) throws ParseException{
			//Create Connection to the Oracle Database
			getConnection();
			Timestamp date =null;
			try{
				System.out.println("get Date Inactive");
				
				String sql="SELECT inactive_dt FROM USAGE_POINTS "
						+"WHERE point='"+pointZABPQ+"' ";
				
				// Create a prepared statement
	 			Statement s = con.createStatement();
				ResultSet rs = s.executeQuery(sql);	
				
				while(rs.next()){ 			
					date=rs.getTimestamp("inactive_dt");		
				}
				if (rs.getString("inactive_dt")==null || rs.getString("inactive_dt").isEmpty()){
					date=null;
				}
				System.out.println("date en base : "+date);
			}
			catch(Exception e){
				System.out.println(e);
			}
			return date;
		}
}
	
